package com.cookman.gameobjects;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.cookman.cards.CityCard;
import com.cookman.gameobjects.decks.CardDeckActor;
import com.cookman.serviceLocators.ServiceLocator;
import com.cookman.utils.AssetUtils;

/**
 * Created by Cookman on 16.09.14.
 */
public class CityCardActor extends AbstractGameObject {

    protected float scale = 1.2f;
    final protected CityCard card;
    private Boolean isSelected = false;

    public CityCard getCard() {
        return card;
    }

    protected Sprite cityCardIcon;

    final CardDeckActor parentDeck;

    public CityCardActor(CityCard card, CardDeckActor parentDeck) {
        super();
        this.card = card;
        this.parentDeck = parentDeck;
        cityCardIcon = AssetUtils.getCityCardSprite(card);
        setBounds(0, 0, scale, scale);
        addListener(mouseActorListener);
        setTouchable(Touchable.enabled);
    }


    @Override
    public void setScale(float scale) {
        this.scale = scale;
    }

    public void setIsSelected(Boolean isSelected) {
        this.isSelected = isSelected;
    }

    public Boolean getIsSelected() {
        return isSelected;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        if (isSelected) {
            batch.draw(cityCardIcon, getX(), getY(), scale * 1.05f, scale * (cityCardIcon.getHeight() / cityCardIcon.getWidth()) * 1.05f);
        } else {
            batch.draw(cityCardIcon, getX(), getY(), scale, scale * (cityCardIcon.getHeight() / cityCardIcon.getWidth()));
        }
    }

    @Override
    public void renderGUI(SpriteBatch batch) {
    }

    @Override
    public void renderShape(ShapeRenderer renderer) {
    }

    private static InputListener mouseActorListener = new InputListener() {
        @Override
        public boolean touchDown(InputEvent event, float x, float y,
                                 int pointer, int button) {
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer,
                            int button) {

            CityCardActor actor = (CityCardActor) event.getTarget();
            System.out.println("City card touched " + actor.card.getName());
            if (actor.parentDeck.isMultiSelect()) {
                if (actor.parentDeck.getSelectedCityCards().size()<actor.parentDeck.getMaxSelected()) {
                    actor.setIsSelected(true);
                    ServiceLocator.getUIController().selectCitiesCards(actor.parentDeck.getSelectedCityCards());
                }
            } else {
                actor.parentDeck.resetSelection();
                actor.setIsSelected(true);
                ServiceLocator.getUIController().selectCityCard(actor);
            }
        }

        @Override
        public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
            super.enter(event, x, y, pointer, fromActor);
            CityCardActor actor = (CityCardActor) event.getTarget();
        }
    };
}
